import axios from "../node_modules/axios/index.js";

declare type IWxRobotMsgPayload = {
    chatid: string
} & ({
    mention?: string,
    payload: {
        msgtype: 'text',
        text: {
            content: string
        }
    }
} | {
    payload: {
        msgtype: 'markdown',
        markdown: {
            content: string
        }
    }
} | {
    payload: {
        msgtype: 'news',
        news: {
            articles: {
                title: string
                description?: string
                url: string
                picurl?: string
            }[]
        }
    }
} | {
    payload: {
        msgtype: 'template_card',
        template_card: {
            card_type: 'text_notice',
            source?: {
                icon_url: string,
                desc: string,
                desc_color: number
            },
            main_title: {
                title: string,
                desc: string
            },
            emphasis_content?: {
                title: string,
                desc: string
            },
            quote_area?: {
                type: 0 | 1 | 2,
                url: string,
                appid: string,
                pagepath: string,
                title: string,
                quote_text: string
            },
            sub_title_text?: string,
            horizontal_content_list?: ({
                keyname: string,
                value: string
            } | {
                keyname: string,
                value: string,
                type: 1,
                url: string
            } | {
                keyname: string,
                value: string,
                type: 2,
                media_id: string,
            } | {
                keyname: string,
                value: string,
                type: 3,
                userid: string
            })[],
            jump_list?: ({
                type: 1,
                url: string,
                title: string
            } | {
                type: 2,
                appid: string,
                pagepath: string,
                title: string
            })[],
            card_action: {
                type: 1,
                url: string
            } | {
                type: 2,
                appid: string,
                pagepath: string
            }
        }
    }
});

export class Postman {

    private static ins: Postman;
    public static get Instance(): Postman {
        if(!Postman.ins) Postman.ins = new Postman();
        return Postman.ins;
    }

    private chatid: string | null = null;
    public setChatId(chatid: string): void {
        this.chatid = chatid;
    }
    
    public async sendTextRobotMsg(type: 'text' | 'markdown', msg: string, mention?: string): Promise<void> {
        if (!this.chatid) return;
        let payload: IWxRobotMsgPayload;
        if (type == 'text') {
            payload = {
                chatid: this.chatid,
                mention,
                payload: {
                    msgtype: 'text',
                    text: {
                        content: msg
                    }
                }
            }
        } else {
            payload = {
                chatid: this.chatid,
                payload: {
                    msgtype: 'markdown',
                    markdown: {
                        content: msg
                    }
                }
            }
        }
        await this.sendAnyRobotMsg(payload);
    }
    
    async sendVIPAD(): Promise<void> {
        if (!this.chatid) return;
        const payload = {
            chatid: this.chatid,
            payload: {
                msgtype: "news",
                news: {
                    articles: [
                        {
                            title: "欢迎使用付费咨询业务",
                            description: "点击体验，早扫早享受",
                            url: "http://builder.fygame.com:3000/static/consulingfee.png",
                            picurl: "http://builder.fygame.com:3000/assets/img/vip.jpg"
                        }
                    ]
                }
            }
        } as IWxRobotMsgPayload;
        await this.sendAnyRobotMsg(payload);
    }
    
    public async sendAnyRobotMsg(payload: IWxRobotMsgPayload): Promise<void> {
        const result = await axios.post<string>('http://builder.fygame.com:3000/api/app/robotmsg', { data: payload }, { timeout: 3000 }).catch(() => {});
        if (result?.data !== 'ok') {
            console.error('robot msg sent failed:', payload);
        }
    }
}